import $http from '../../http/http.js'
import apiSetting from '../../http/apiSetting.js'
let dayjs = require('dayjs');
let app = getApp();

Page({
  data: {
    delBtnShow: true,
    type: 'add',
    isdaterange: false,
    startDate: '',
    endDate: '',
    showEffectDate: '',
    isTimePas: false,
    beginTime: '00:00',
    endTime: '00:00',
    showEffectTime: '',
    assignType: 'date',
    weekArray: [{
      name: "星期一",
      value: 1
    }, {
      name: "星期二",
      value: 2
    }, {
      name: "星期三",
      value: 3
    }, {
      name: "星期四",
      value: 4
    }, {
      name: "星期五",
      value: 5
    }, {
      name: "星期六",
      value: 6
    }, {
      name: "星期日",
      value: 7
    }],
    weekIndex: 0,
    form: {
      beginDatetime: "",
      beginTime: "",
      dayOfWeek: 1,
      description: "",
      endDatetime: "",
      endTime: "",
      ftyModelId: "",
      ftyModelValue: "",
      name: "",
      type: 1,
    }
  },
  onLoad(query) {
    console.log(query.type)
    this.data.query = query
    if (query.type == 'add') {
      dd.setNavigationBar({
        title: "新增停产计划"
      })
      this.setData({
        delBtnShow: false
      })
      this.getStorage()
    }
    if (query.type == 'edit') {
      this.factoryCalendarPlanGetById(query.id);
      dd.setNavigationBar({
        title: "停产计划编辑"
      })
      this.setData({
        delBtnShow: true
      })

    }
    this.setData({
      type: query.type
    })
  },

  onShow() {
    if (this.data.query.type == 'edit') {
      this.getStorage()
    }
  },

  selectType(e) {
    let type = e.target.dataset.type
    if (type == 1) {
      this.setData({
        startDate: '',
        endDate: '',
        beginTime: '00:00',
        endTime: '00:00',
        showEffectTime: ''
      })
    }
    if (type == 2) {
      this.setData({
        startDate: '',
        endDate: '',
        showEffectDate: '',
        weekIndex: 0,
        "form.dayOfWeek": 1
      })
    }
    this.setData({
      "form.type": type
    })
  },

  selectDate() {
    this.setData({
      isdaterange: true
    })
  },

  selectTime() {
    this.setData({
      isTimePas: true
    })
  },

  weekPickerChange(e) {
    this.setData({
      weekIndex: e.detail.value,
      "form.dayOfWeek": this.data.weekArray[e.detail.value].value
    })
  },

  // 日期选择返回
  onAffirm(data) {
    console.log(data)
    if (data.result) {
      this.setData({
        "form.beginDatetime": data.startDate,
        "form.endDatetime": data.endDate,
        startDate: data.startDate,
        endDate: data.endDate,
        showEffectDate: data.startDate + "至" + data.endDate
      })
    }
    this.setData({
      isdaterange: false
    })
  },

  // 时间选择返回
  onSelectTime(data) {
    console.log(data)
    if (data.result) {
      this.setData({
        "form.beginTime": data.startTime,
        "form.endTime": data.endTime,
        beginTime: data.startTime,
        endTime: data.endTime,
        showEffectTime: data.startTime + "至" + data.endTime
      })
    }
    this.setData({
      isTimePas: false
    })
  },

  // 编辑页面获取数据
  factoryCalendarPlanGetById(id) {
    let that = this
    $http(apiSetting.factoryCalendarPlanGetById, {
      id: id,
    }).then((data) => {
      console.log(data.result)
      that.setData({
        form: data.result
      })
      if (data.result.type == 1) {
        that.setData({
          showEffectDate: dayjs(data.result.beginDatetime).format('YYYY-MM-DD') + "至" + dayjs(data.result.endDatetime).format('YYYY-MM-DD'),
          startDate: dayjs(data.result.beginDatetime).format('YYYY-MM-DD'),
          endDate: dayjs(data.result.endDatetime).format('YYYY-MM-DD'),
        })
      }
      if (data.result.type == 2) {
        let arr1 = data.result.beginTime.split(':')
        let arr2 = data.result.endTime.split(':')
        that.setData({
          beginTime: arr1[0] + ':' + arr1[1],
          endTime: arr2[0] + ':' + arr2[1],
          showEffectTime: arr1[0] + ':' + arr1[1] + '至' + arr2[0] + ':' + arr2[1],
        })
      }
      dd.getStorage({
        key: 'selectFacScope',
        success: function (res) {
          that.data.selectFacScope = res.data[0]
          that.setData({
            'form.ftyModelName': res.data[0].title
          })
        },
      });
    })
  },

  save() {
    let that = this;
    let beginDatetime = ""
    let endDatetime = ""
    let beginTime = ''
    let endTime = ''
    if (that.data.form.type == 1) {
      if (that.data.showEffectDate == '') {
        dd.alert({ content: "请选择指定日期" });
        return
      }
      beginDatetime = dayjs(that.data.form.beginDatetime).startOf('day').format('YYYY-MM-DD HH:mm:ss')
      endDatetime = dayjs(that.data.form.endDatetime).endOf('day').format('YYYY-MM-DD HH:mm:ss')
    }
    if (that.data.form.type == 2) {
      if (that.data.showEffectTime == '') {
        dd.alert({ content: "请选择指定时间段" });
        return
      }
      let arr1 = that.data.form.beginTime.split(':')
      let arr2 = that.data.form.endTime.split(':')
      beginTime = arr1[0] + ":" + arr1[1] + ":00"
      endTime = arr2[0] + ":" + arr2[1] + ":00"
    }
    console.log(that.data.form)
    if (that.data.type == 'add') {
      // 新增保存
      $http(apiSetting.factoryCalendarPlanAdd, {
        beginDatetime: beginDatetime, //开始⽣效⽇期
        beginTime: beginTime, //开始时间
        dayOfWeek: that.data.form.dayOfWeek, //周⼏，周⼀⾄周⽇(1-7)
        description: that.data.form.description, //描述
        endDatetime: endDatetime, //结束⽣效⽇期
        endTime: endTime, //结束时间
        ftyModelId: that.data.form.ftyModelId, //⼯⼚模型ID
        ftyModelValue: that.data.form.ftyModelValue, //⼯⼚模型层级值，⼯⼚1，⻋间2，产线3，加⼯中⼼4，设备5
        name: that.data.form.name, //名称
        type: that.data.form.type //计划停产管理类型，指定 时间段1，按星期2
      }).then((data) => {
        if (data.code == 0) {
          const pages = getCurrentPages();
          let currPage = pages[pages.length - 1]; //当前页面
          let prevPage = pages[pages.length - 2]; //上一个页面
          prevPage.data.isGetAfreshData = true;
          dd.navigateBack({
            delta: 1
          })
        }
      })
    } else {
      // 更新保存
      $http(apiSetting.factoryCalendarPlanUpdate, {
        id: that.data.form.id,
        beginDatetime: beginDatetime, //开始⽣效⽇期
        beginTime: beginTime, //开始时间
        dayOfWeek: that.data.form.dayOfWeek, //周⼏，周⼀⾄周⽇(1-7)
        description: that.data.form.description, //描述
        endDatetime: endDatetime, //结束⽣效⽇期
        endTime: endTime, //结束时间
        ftyModelId: that.data.form.ftyModelId, //⼯⼚模型ID
        ftyModelValue: that.data.form.ftyModelValue, //⼯⼚模型层级值，⼯⼚1，⻋间2，产线3，加⼯中⼼4，设备5
        name: that.data.form.name, //名称
        type: that.data.form.type //计划停产管理类型，指定 时间段1，按星期2
      }).then((data) => {
        if (data.code == 0) {
          const pages = getCurrentPages();
          let currPage = pages[pages.length - 1]; //当前页面
          let prevPage = pages[pages.length - 2]; //上一个页面
          prevPage.data.isGetAfreshData = true;
          dd.navigateBack({
            delta: 1
          })
        }
      })
    }
  },

  // 选择范围
  selectRange() {
    dd.navigateTo({
      url: '/pages/selectRange/selectRange?type=back'
    })
  },

  // 删除
  delete() {
    let that = this
    dd.confirm({
      title: '温馨提示',
      content: '删除不可恢复，你确定要删除？',
      confirmButtonText: '确定',
      cancelButtonText: '取消',
      success: (result) => {
        if (result.confirm == true) {
          $http(apiSetting.factoryCalendarPlanDelete, {
            id: that.data.form.id //计划停产记录ID
          }).then((data) => {
            console.log(data)
            // 删除成功后，返回上个页面
            dd.navigateBack({
              delta: 1
            })
          })
        }
      },
    });
  },

  onInputName(e) {
    this.data.form.name = e.detail.value
  },
  descriptionInp(e) {
    this.data.form.description = e.detail.value
  },

  getStorage() {
    let that = this
    dd.getStorage({
      key: 'selectFacScope',
      success: function (res) {
        that.data.selectFacScope = res.data[0]
        that.setData({
          'form.ftyModelId': res.data[0].key,
          'form.ftyModelName': res.data[0].title,
          'form.ftyModelValue': res.data[0].level + 1,
        })
      },
    });
  }
});
